<template>
  <div class="login">
    <div class="login_box">
      <!-- login -->
      <div class="login_logo">
        <img src="@/assets/images/logo.png" alt />
      </div>
      <!-- 表单 -->
      <el-form ref="loginFormRef" class="form" :model="loginForm" :rules="loginRules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 用户密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            type="password"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="loginBtn">登陆</el-button>
          <el-button type="info" @click="loginReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 用户登录Api
import { getUserLoginApi } from '@/api/user.js'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        // 用户名称效验
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        // 用户密码效验
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 11, message: '长度在 6 到 11 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginReset() {
      this.$refs.loginFormRef.resetFields()
    },
    loginBtn() {
      this.$refs.loginFormRef.validate(val => {
        if (!val) return
        getUserLoginApi(this.loginForm).then(res => {
          if (res.data.meta.status === 200) {
            this.$message.success('登陆成功')
            sessionStorage.setItem('tokenUser', res.data.data.token)
            this.$router.push('/home')
          } else {
            this.$message.error('登陆失败')
          }
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .login_logo {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 10px;
    width: 130px;
    height: 130px;
    border-radius: 50%;
    background-color: #fff;
    border: 1px solid #eee;
    box-shadow: 0 0 10px #ddd;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      border: 1px solid #eee;
      background-color: #eee;
    }
  }
}

.form {
  position: absolute;
  bottom: 0;
  padding: 0 20px;
  width: 100%;
  box-sizing: border-box;
}

.btn {
  display: flex;
  justify-content: flex-end;
}
</style>
